package handler

import (
	"github.com/gin-gonic/gin"
	"pos-master/models"
	"strconv"
)

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))

	offset := (page - 1) * size
	models.DB.Offset(offset).Limit(size)
	var news []models.ZW
	models.DB.Find(&news)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": news,
	})
	return
}

func Detail(c *gin.Context) {
	id := c.Query("id")
	var news models.ZW
	models.DB.Where("id =?", id).Find(&news)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": news,
	})
	return
}

func Like(c *gin.Context) {
	id := c.Query("id")

	var news models.ZW
	err := models.DB.Where("id =？", id).Find(news).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "收藏成功",
	})
	return
}
